<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link rel="stylesheet" type="text/css" href="../css/common.css" />
	</head>

	<body>
		<div class="jcontent">
			<img src="../img/zw.png" id="img" class="img" />
		</div>
		<link rel="stylesheet" type="text/css" href="../js/cropper/cropper.css" />
		<script src="../js/mui.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="../js/common.js" type="text/javascript" charset="utf-8"></script>
		<link rel="stylesheet" href="../js/cropper/cropper.min.css">
		<script type="text/javascript" src="../js/jquery.min.js"></script>
		<script type="text/javascript" src="../js/cropper/cropper.min.js"></script>

		<script type="text/javascript">
			mui.plusReady(function() {
				var imgpath = webapp.GetQueryString("img");
//				$('#img').attr("src", imgpath);
				document.getElementById("img").setAttribute("src",imgpath);
				initZoomImg();
			});

			window.addEventListener("riconClick", function(e) {
				uploadUserImg();
			});

			function initZoomImg() {
				var $image = $('#img');
				var options = {
					aspectRatio: 16 / 16,
					resizable: false,
					zoomable: false,
					touchDragZoom: false,
					dragCrop: false
				};
				// 初始化Cropper
				$image.on({}).cropper(options);
			}

			function getNewImgDataBase64() {
				var $image = $('#img');
				var newImg = $image.cropper("getCroppedCanvas");
				return newImg.toDataURL();
			}

			function uploadUserImg() {
				webapp.showWaiting();
				//字段校验成功 发起请求 保存数据
				webapp.ajax({
					url: "/carLife/admin/interface/carOwner/update/updateHeadPortrait",
					type:"post",
					data: {"base64Str": getNewImgDataBase64()},
					success: function(json) {
						webapp.debug(json);
						webapp.closeWaiting();
						if(json.code == '200') {
							mui.toast("上传成功！");
							//如果点了更新按钮 个人信息发生变化 需要重新加载页面数据
							var _user=webapp.getUser();
							_user["headPortrait"]=json.data.relativeUrl;
							var serilizeUser=JSON.stringify(_user);
							//把用户对象放入缓存
							window.localStorage.setItem(webapp.config.CACHE_USER,serilizeUser);
							//
							var gerenwebview = plus.webview.getWebviewById("common-sub");
							mui.fire(gerenwebview, 'userInfoInit', {path:json.data.relativeUrl});
						}
					}
				});
			}
		</script>
	</body>

</html>